﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_MaintainCompetitions : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
            if (Request["CompetitionID"] != null)
            {
                loadDetails();
            }
            if (!IsPostBack) loadCompetitions();        
    }
    public void loadCompetitions()
    {
        try
        {
            string typeCompetition = Request["typeCompetition"];
            CompetitionDAO comp = new CompetitionDAO();

            if (typeCompetition == "All" || typeCompetition == null)
                grv_Competitions.DataSource = comp.GetAll();

            else if (typeCompetition == "Upcoming")
                grv_Competitions.DataSource = comp.SelectUpcoming();

            else if (typeCompetition == "Ongoing")
                grv_Competitions.DataSource = comp.SelectOngoing();

            else if (typeCompetition == "Completed")
                grv_Competitions.DataSource = comp.SelectPrevious();

            grv_Competitions.DataBind();

            foreach (GridViewRow row in grv_Competitions.Rows)
            {
                Repeater datalist = (Repeater)row.FindControl("dtl_Awards");
                int competitionsID = int.Parse(grv_Competitions.DataKeys[row.RowIndex].Value.ToString());
                AwardDAO ar = new AwardDAO();
                datalist.DataSource = ar.AwardOfCompetition(competitionsID);
                datalist.DataBind();
            }
        }
        catch (Exception ex) { }
    }
    public void loadDetails()
    {
        try
        {
            Control ct;
            ct = Page.LoadControl("~/Share/CompetitionDetails.ascx");
            pn_DetailCompetition.Controls.Add(ct);
        }
        catch (Exception ex) { }
     
    }
    protected void btn_UpcomingCompetitions_Click(object sender, EventArgs e)
    {

        Response.Redirect("MaintainCompetitions.aspx?typeCompetition=Upcoming");
    }
    protected void btn_OngoingCompetitions_Click(object sender, EventArgs e)
    {   
        Response.Redirect("MaintainCompetitions.aspx?typeCompetition=Ongoing");
    }
    protected void btn_AllCompetitions_Click(object sender, EventArgs e)
    {
        Response.Redirect("MaintainCompetitions.aspx?typeCompetition=All");
    }
    
    protected void test(object sender, EventArgs e)
    {
       
    }
    protected void btn_Insert_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/InsertCompetition.aspx");
    }

    protected void btn_CompletedCompetitions_Click(object sender, EventArgs e)
    {
        Response.Redirect("MaintainCompetitions.aspx?typeCompetition=Completed");
    }
    protected void grv_Competitions_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Competitions.PageIndex = e.NewPageIndex;
        loadCompetitions();
    }

    public void onCheckedChange(object sender, EventArgs e)
    {
        if (((CheckBox)sender).Checked)
            foreach (GridViewRow row in grv_Competitions.Rows)
            {
                ((CheckBox)row.FindControl("chk_Item")).Checked = true;
            }
        else
        {
            foreach (GridViewRow row in grv_Competitions.Rows)
            {
                ((CheckBox)row.FindControl("chk_Item")).Checked = false;
            }
        }
    }

    protected void grv_Competitions_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        
    }

    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            CompetitionInfo competInfo = new CompetitionInfo();
            CompetitionDAO competDAO = new CompetitionDAO();
        PostingDAO postDAO = new PostingDAO();
       
            bool ischeck = false;

            foreach (GridViewRow row in grv_Competitions.Rows)
            {
                if (((CheckBox)row.FindControl("chk_Item")).Checked == true)
                {
                    // ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Please choose the exhibition to delete!');", true);
                    ischeck = true;
                }

            }

            if (ischeck == true)
            {
                foreach (GridViewRow row in grv_Competitions.Rows)
                {
                    int competitionsID = int.Parse(grv_Competitions.DataKeys[row.RowIndex].Value.ToString());

                    if (((CheckBox)row.FindControl("chk_Item")).Checked == true)
                    {

                         IList<PostingInfo> list = postDAO.GetAllPostingByCompetID(competitionsID);
                       // 
                        if(list.Count > 0)
                            if (DeleteAllPostByCompetID(competitionsID) > 0)
                                if (competDAO.DeleteCompetition(competitionsID) > 0)
                                {
                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete successfull!');", true);
                                }
                                else
                                {
                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete Failed!');", true);
                                }
                            else
                            {
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete All Posting Failed!');", true);
                            }
                        else if (competDAO.DeleteCompetition(competitionsID) > 0)
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete Successfull!');", true);
                        }
                    }
                    
                }
                loadCompetitions();
            }
            else { ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Please choose the Competition to delete!');", true); }
        }
       catch (Exception ex) { }


    }

    public int DeleteAllPostByCompetID(int competID)
    {

            PostingDAO postDAO = new PostingDAO();
            int result = postDAO.DeletePostingByCompetID(competID);

            return result;
     }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        try
        {
            CompetitionDAO comp = new CompetitionDAO();
            grv_Competitions.DataSource = comp.GetCompetitionName("%" + txtSearch.Text + "%");
            grv_Competitions.DataBind();

            foreach (GridViewRow row in grv_Competitions.Rows)
            {
                Repeater datalist = (Repeater)row.FindControl("dtl_Awards");
                int competitionsID = int.Parse(grv_Competitions.DataKeys[row.RowIndex].Value.ToString());
                AwardDAO ar = new AwardDAO();
                datalist.DataSource = ar.AwardOfCompetition(competitionsID);
                datalist.DataBind();
            }
        }
        catch (Exception ex) { }
    }


}